home *** CD-ROM | disk | FTP | other *** search
/ Dirty Old Coins - Cleani…ons & Coin Image Database / Dirty Old Coins - Cleaning Instructions and Coin Image Database.iso / data / sf.js < prev    next >
Encoding:
JavaScript  |  2002-03-22  |  12.4 KB  |  371 lines

  1. /*
  2. This search routine is based on the free Recon Search Engine
  3. by Jerry Bradenbaugh (http://www.serve.com/hotsyte/)
  4. alterations have been made by Marc Reed (http://www.marcreed.com)
  5. This script may be used for free
  6. */
  7.  
  8. // Define global variables
  9.  
  10. // the maxPages variable determines how
  11. // many results are displayed per page:
  12. var maxPages = 10;
  13.  
  14. var allMatch = new Array();
  15. var phraseMatch = new Array();                            
  16. var anyMatch = new Array();
  17. var urlMatch = new Array();
  18. var indexer = 0;
  19. var all = false;
  20. var count = 0;
  21. var start = 0;
  22. var urlTest = false;
  23.  
  24.  
  25. //Search Results page should call this function
  26. function doSearch() {
  27.  
  28.     fullEntry = getQueryString('searchField');
  29.     filteredEntry = ""
  30.     //Look for ++(clean up multiple spaces)
  31.     var PlusSigns = "\+\+"
  32.     while (fullEntry.indexOf(PlusSigns) >= 0) {
  33.         fullEntry=fullEntry.replace(/\+\+/, "+")
  34.     }
  35.     
  36.     //Isolate search term(s) and convert "+" to spaces
  37.     for (i=0; i<fullEntry.length; i++) {
  38.         if ((fullEntry.charAt(i) == "&")||(fullEntry.charAt(i) == "#")) {
  39.             break
  40.         }
  41.         if (fullEntry.charAt(i) == "+") {
  42.             filteredEntry = (filteredEntry+" ")
  43.         }else {
  44.             filteredEntry = (filteredEntry+(fullEntry.charAt(i)))
  45.         }
  46.     }
  47.     
  48.     if (filteredEntry.indexOf("#") > -1) filteredEntry = filteredEntry.substring(0, filteredEntry.indexOf("#"));
  49.     validate(filteredEntry);
  50. }
  51.  
  52. //This generic function will return the value of a QueryString
  53. function getQueryString(Val) {
  54.     thisURLparamStr = document.location.search;
  55.     //chop "?" off thisURLparamStr
  56.     if (thisURLparamStr.charAt(0) == "?") thisURLparamStr = thisURLparamStr.substring(1, thisURLparamStr.length);
  57.     returnStr = "";
  58.     if (thisURLparamStr != "") {
  59.         //Build array out of thisURLparamStr using "&" as delimiter
  60.         divide1=(thisURLparamStr.split("&"))
  61.         for (i=0; i < divide1.length; i++) {
  62.             divide2 = divide1[i].split("=")
  63.             if (unescape(divide2[0]) == Val) {
  64.                 returnStr = unescape(divide2[1]);
  65.             }
  66.         }
  67.     }
  68.     return returnStr;
  69. }
  70.  
  71. //Determine any word/all words/phrase search
  72. function validate(text) {
  73.  
  74.     var entry = text;
  75.     
  76.     //clean up spaces at beginning of string
  77.     while (entry.charAt(0) == ' ') {                    
  78.         entry = entry.substring(1,entry.length);
  79.     }
  80.     //clean up spaces at end of string
  81.     while (entry.charAt(entry.length - 1) == ' ') {
  82.         entry = entry.substring(0,entry.length - 1);
  83.     } 
  84.     
  85.     if ((entry.charAt(0) == "+")||(getQueryString('srcriteria') == "all")) {
  86.         entry = (entry.charAt(0) == "+")? entry.substring(1,entry.length):entry;
  87.         all = true;
  88.     }
  89.     if ((entry.substring(0,4) == "url:") && (all == false)) {
  90.         entry = entry.substring(5,entry.length);
  91.         all = null;
  92.     }
  93.     //If user wants exact phrase
  94.     if (((entry.charAt(0) == "\"")&&(entry.charAt((entry.length)-1) == "\""))||(getQueryString('srcriteria') == "phrase")){
  95.         while (entry.indexOf("\"") > -1) {
  96.             entry=entry.replace(/\"/, "")
  97.         }
  98.         all = 3;
  99.     }
  100.     if (entry.length < 3) {
  101.         alert("Please type a word larger than three characters.");
  102.         return;
  103.     }
  104.     convertString(entry, text);
  105. }
  106.  
  107.  
  108. //*************************************************************
  109. // This function merges title, brief description, page content,
  110. // keywords and returns all-caps string
  111. //*************************************************************
  112. function mergeCaps(str1, str2, str3) {
  113.  
  114.     mergeStr = "";
  115.     // join str2 (brief description) and str3 (rest of page text)
  116.     // if page content is longer than brief description length,
  117.     // then str2 ends "..."
  118.     // if str2 ends "..." remove dots
  119.     if ((str2.length > 0) && (str2.charAt(0) != " ")) str2 = " " + str2;
  120.     if (str2.substring(str2.length - 3, str2.length) == "...") {
  121.             mergeString = str2.substring(0, str2.length - 3).concat(str3);
  122.     } else {
  123.             mergeString = str2.concat(str3);
  124.     }
  125.     mergeString = str1 + mergeString
  126.     //to make search case-insensitive, convert to all caps
  127.     return mergeString.toUpperCase();
  128.  
  129. }
  130.  
  131. //*************************************************************************
  132. // This function will parse the URL search string and change a name/value pair
  133. //*************************************************************************
  134. function changeParam(whichParam, newVal) {
  135.     newParamStr = "";
  136.     thisURLstr = document.location.href.substring(0, document.location.href.indexOf("?"));
  137.     thisURLparamStr = document.location.href.substring(document.location.href.indexOf("?") + 1, document.location.href.length);
  138.     //Build array out of thisURLparamStr using "&" as delimiter
  139.     divide1=(thisURLparamStr.split("&"))
  140.     for (cnt=0; cnt < divide1.length; cnt++) {
  141.         divide2 = divide1[cnt].split("=")
  142.         if (divide2[0] == whichParam) {
  143.             // if we find whichParam in thisURLparamStr replace whichParam's value with newVal
  144.             newParamStr = newParamStr + divide2[0] + "=" + escape(newVal) + "&";
  145.         } else {
  146.             //leave other parameters intact
  147.             newParamStr = newParamStr + divide2[0] + "=" + divide2[1] + "&";
  148.         }
  149.     }
  150.     //strip off trailing ampersand
  151.     if (newParamStr.charAt(newParamStr.length - 1) == "&") newParamStr = newParamStr.substring(0, newParamStr.length - 1);
  152.     //apply new URL
  153.      return(thisURLstr + "?" + newParamStr);
  154. }
  155.  
  156.  
  157. //*************************************************************
  158. // Sorts search results based on 1.Number of hits 2.aplhabetically
  159. //*************************************************************
  160. function compare(a, b) {
  161.     if (parseInt(a) - parseInt(b) != 0) {
  162.         return parseInt(a) - parseInt(b)
  163.     }else {
  164.         var aComp = a.substring(a.indexOf("|") + 1, a.length)
  165.         var bComp = b.substring(b.indexOf("|") + 1, b.length)
  166.         if (aComp < bComp) {return -1}
  167.         if (aComp > bComp) {return 1}
  168.         return 0
  169.     }
  170. }
  171.  
  172. // If not exact phrase, split the entry string into an array
  173. function convertString(reentry, text) {
  174.         if (all == 3) {
  175.             searchTerm = reentry;
  176.             requirePhrase(searchTerm, text);
  177.             return;
  178.         }
  179.         searchArray = reentry.split(" ");
  180.         if (all == false) { allowAny(searchArray, text); return; }
  181.         if (all == true) { requireAll(searchArray, text); return; }
  182.         if (all == null) { parseURL(searchArray, text); return; }
  183.         //If user wants exact phrase
  184. }
  185.  
  186. //Evoked if user searches ANY WORDS
  187. function allowAny(t, text) {
  188.     for (i = 0; i < profiles.length; i++) {
  189.         //strip url out of search string
  190.         var refineElement = "";
  191.         var splitline = profiles[i].split("|");
  192.         refineElement = mergeCaps(splitline[0], splitline[1], splitline[2])
  193.         var OccurNum = 0;
  194.         for (j = 0; j < t.length; j++) {
  195.             var compareString = t[j].toUpperCase();
  196.             if (refineElement.indexOf(compareString) != -1) {    // If a match is found,    
  197.                 //if a match is found determine how many occurances are in refineElement 
  198.                 var searchStrArray = refineElement.split(" ")
  199.                 for (k = 0; k < searchStrArray.length; k++) {
  200.                     if (searchStrArray[k].indexOf(compareString) > -1) OccurNum++
  201.                 }
  202.             }
  203.         }
  204.         if (OccurNum > 0) {
  205.             anyMatch[indexer] = (0-OccurNum) + "|" + profiles[i];
  206.             indexer++;
  207.         }
  208.     }
  209.  
  210.     if (anyMatch.length == 0) {                        // If no matches are found, print a no match
  211.         noMatch(text);                            // HTML document
  212.         return;
  213.     }
  214.     else { formatResults(anyMatch, text); }                    // Otherwise, generate a results document
  215. }
  216.  
  217. //Evoked if user searches ALL WORDS
  218. function requireAll(t, text) {
  219.     for (i = 0; i < profiles.length; i++) {
  220.         var allConfirmation = true;
  221.         var refineAllString = "";
  222.         var splitline = profiles[i].split("|");
  223.         refineAllString = mergeCaps(splitline[0], splitline[1], splitline[2])
  224.         var OccurNum = 0;
  225.         for (j = 0; j < t.length; j++) { 
  226.             var allElement = t[j].toUpperCase();
  227.             var searchStrArray = refineAllString.split(" ");
  228.             for (k = 0; k < searchStrArray.length; k++) {
  229.                 if (searchStrArray[k].indexOf(allElement) > -1) OccurNum++
  230.             }
  231.             if (refineAllString.indexOf(allElement) == -1) { 
  232.                 allConfirmation = false;
  233.                 continue; 
  234.                 }    
  235.             }
  236.         if (allConfirmation) {
  237.             allMatch[indexer] = (0-OccurNum) + "|" + profiles[i];
  238.             indexer++;
  239.             }
  240.         }
  241.     if (allMatch.length == 0) {
  242.         noMatch(text);
  243.         return;
  244.         }
  245.     else { formatResults(allMatch, text); }
  246.     }
  247.  
  248.  
  249.  
  250. //If user wants exact phrase
  251. function requirePhrase(t, text) {
  252.     for (i = 0; i < profiles.length; i++) {
  253.         var allConfirmation = true;
  254.         //strip url out of search string
  255.         var refineAllString = "";
  256.         var splitline = profiles[i].split("|");
  257.         refineAllString = mergeCaps(splitline[0], splitline[1], splitline[2])
  258.         var allElement = t.toUpperCase();
  259.         var OccurNum = 0;
  260.         if (refineAllString.indexOf(allElement) > -1) {
  261.             var tempLine = refineAllString;
  262.             while (tempLine.indexOf(allElement) > -1) {
  263.                 OccurNum++
  264.                 tempLine = tempLine.substring(tempLine.indexOf(allElement) + allElement.length, tempLine.length);
  265.             }
  266.             phraseMatch[indexer] = (0-OccurNum) + "|" + profiles[i];
  267.             indexer++;
  268.         }
  269.     }
  270.  
  271.     if (phraseMatch.length == 0) {
  272.         noMatch(text);
  273.         return;
  274.     }
  275.     else { formatResults(phraseMatch, text); }
  276. }
  277.  
  278.  
  279.  
  280. function parseURL(u, text) {                                 // Incite the search, looking only in the URL portion of the string
  281.     for (i = 0; i < profiles.length; i++) {        
  282.         var urlConfirmation = true;        
  283.         var anyURL = profiles[i].toUpperCase();
  284.         //strip url out of search string
  285.         var splitline = anyURL.split("|");
  286.         var refineAnyURL = splitline[splitline.length - 1];
  287.         var OccurNum = 0;
  288.         for (j = 0; j < u.length; j++) { 
  289.             var urlPart = u[j].toUpperCase();
  290.             if (refineAnyURL.indexOf(urlPart) != -1 && (urlConfirmation)) { 
  291.                 urlConfirmation = false;
  292.                 urlMatch[indexer] = (0-OccurNum) + "|" + profiles[i];
  293.                 indexer++;
  294.             }
  295.         }
  296.     }
  297.     if (urlMatch.length == 0) {
  298.         noMatch(text);
  299.         return;
  300.     }
  301.     else {
  302.         urlTest = true;
  303.         formatResults(urlMatch, text, urlTest);
  304.     }
  305. }
  306.  
  307. //*************************************************************
  308. // Format no-results page
  309. //*************************************************************
  310. function noMatch(text) {                                // Dyanmic HTML page with no results
  311.     document.writeln("<a name=\"top_of_page\"></a><h3>Search Results</h3>");
  312.     document.writeln("<h4><hr size=\"1\"><b>'" + text + "' returned no results.<hr size=\"1\"><h4>");
  313.     return true;
  314.     }
  315.  
  316. //*************************************************************
  317. // Format successfull search results page
  318. //*************************************************************
  319. function formatResults(passedArray, text, urlTest) {
  320.     results = passedArray;
  321.     pgRange = (getQueryString("range") != "")? parseInt(getQueryString("range")):1;
  322.     document.writeln("<a name=\"top_of_page\"></a><h3>Search Results</h3>");
  323.     document.writeln("<h4><hr size=\"1\">Search Query: " + text + "<br>");
  324.     document.writeln("Search Results: "+ passedArray.length + "");
  325.     document.writeln("<hr size=\"1\"></h4>");
  326.     thisPg = 1;
  327.     endPg = passedArray.length;
  328.     if (passedArray.length > maxPages) {
  329.         thisPg = (maxPages * pgRange) - (maxPages - 1);
  330.         endPg = (parseInt(thisPg + (maxPages - 1)) < passedArray.length)? parseInt(thisPg + (maxPages - 1)):passedArray.length;
  331.         document.writeln(thisPg + " - " + endPg + " of " + passedArray.length);
  332.     }
  333.     document.writeln("<dl>");
  334.     passedArray.sort(compare);
  335.     if (urlTest) {
  336.         for (i = 0; i < passedArray.length ; i++) {
  337.             divide = passedArray[i].split("|");             // Print each URL result as a unit of a definition list
  338.             document.writeln("<dt>" + "<a href=\""+divide[4]+ "\" target=\"_self\">" + divide[4] + "</a></dt>");
  339.             document.writeln("<dd>" + divide[2] + "</dd><br><br>");
  340.         }
  341.     }
  342.     else {
  343.         for (i = (thisPg - 1); i < endPg; i++) {
  344.             divide = passedArray[i].split("|");             // Print each profile result as a unit of a definition list
  345.             document.writeln("<dt><a href=\""+divide[4]+"\" target=\"_self\"><b>" + divide[1] + "</b></a><\dt>");
  346.             document.writeln("<dd>" + divide[2] + "</dd><br><br>");
  347.         }
  348.     }
  349.     document.writeln("</dl>");                // Finish the HTML document
  350.  
  351.     //write results page numbers
  352.     if (passedArray.length > maxPages) {
  353.         pgNum = parseInt(passedArray.length/maxPages);
  354.         if (passedArray.length/maxPages > pgNum) pgNum++;
  355.         pgLinks = "go to page: ";
  356.         for (i=0; i < pgNum; i ++) {
  357.             locationStr = (location.href.indexOf("&range=") > -1)? changeParam("range", parseInt(i + 1)):location.href + "&range=" + parseInt(i + 1);
  358.             pgLinks += (parseInt(i + 1) != pgRange)? "<a href=\"" + locationStr + "\">" + (i + 1) + "</a> ":"<b>" + (i + 1) + "</b> ";
  359.         }
  360.         document.writeln(pgLinks + "<hr size=\"1\">");
  361.     } 
  362.     clearOut();
  363. }
  364.  
  365. function clearOut() {                                // Clear the arrays and variables generated from the current search
  366.     allMatch.length = 0;    anyMatch.length = 0;
  367.     urlMatch.length = 0;    divide.length = 0;
  368.     indexer = 0;    all = false;     urlTest = false;
  369. }
  370.     
  371.